/**
 * 2017年3月1日
 */
package controllers.wechatlab;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import controllers.BaseController;
import pojo.weichatlab.figure.AbstractChart;
import pojo.weichatlab.figure.LineChart;
import pojo.weichatlab.figure.MapChart;
import pojo.weichatlab.figure.PieChart;
import pojo.weichatlab.figure.StackedColumnChart;

/**
 * @author Alex
 *
 */
@Controller
@RequestMapping("sparkol")
public class SparkController extends BaseController{
	/**
	 * 
	 */
	public SparkController() {
		// TODO Auto-generated constructor stub
//		AbstractChart.fromRequest(=, clzz)
		AbstractChart.om = mapper;
	}
	@RequestMapping("/index.do")
	public ModelAndView index(HttpServletRequest req,HttpServletResponse res){
		
		return renderView("sparkol", null);
	}
	@RequestMapping("/highcharts.do")
	public ModelAndView highcharts(HttpServletRequest req,HttpServletResponse res){
		Map<String,Object> map = new HashMap<>();
		String type = (String) req.getParameter("type");
		
		map.put("figureType",type);
		if(map.get("figureType").equals("line") || map.get("figureType").equals("column")){
			LineChart lc = AbstractChart.fromRequest(req, LineChart.class);
			map.put("sName",lc.sName);
			map.put("title",lc.title);
			map.put("subTitle",lc.subTitle);
			map.put("x", lc.x);
			map.put("y", lc.y);
			map.put("valueSuffix", lc.valueSuffix);
		}else{
			
			//饼图
			//data所显示的数据应该是数组的数组,也可以是对象({}表示对象)的数组
			//例如:[ ['Firefox',   45.0],['IE',       26.8],{name: 'Chrome',y: 12.8,sliced: true,selected: true},['Safari',    8.5],['Opera',     6.2],['Others',   0.7]]
			PieChart pc = AbstractChart.fromRequest(req, PieChart.class);
			map.put("title",pc.title);
			map.put("subTitle",pc.subTitle);
			ArrayList<List> l = new ArrayList<>();
			for(String key:pc.ratio.keySet()){
				List<String> t = new ArrayList<>();
				t.add(key);
				t.add(String.valueOf(pc.ratio.get(key)));
				l.add(t);
			}
			map.put("y", l);
		}
	
		return renderView("figure/highcharts", map);
	}
	@RequestMapping("/highmaps.do")
	public ModelAndView highmaps(HttpServletRequest req,HttpServletResponse res){
		MapChart mc = AbstractChart.fromRequest(req, MapChart.class);
		return renderView("figure/highmaps", null)
				.addObject("provincesData", mc.data)
				.addObject("title",mc.title)
				.addObject("subTitle",mc.subTitle)
				.addObject("tooltipStr",mc.tooltipStr);
	}
	
	@RequestMapping("/stackedcolumn.do")
	public ModelAndView stackedcolumn(HttpServletRequest req,HttpServletResponse res){
		StackedColumnChart sccc = AbstractChart.fromRequest(req, StackedColumnChart.class);
		return renderView("figure/highcharts-stackedcolumn", null)
				.addObject("title",sccc.title)
				.addObject("x", sccc.x)
				.addObject("y",sccc.y)
				.addObject("z",sccc.z)
				.addObject("subTitle",sccc.subTitle);
	}
	
	
}
